package com.project.gossip;

import java.util.Date;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.project.gossip.flyoutmenu.FlyOutContainer;
import com.project.gossip.recyclerview.GossipAdapter;
import com.project.gossip.recyclerview.GossipManager;

public class NewGossipActivity extends ActionBarActivity {

	public static final String PREFS_NAME = "MyGossip";
	private static final int CONTACT_PICKER_RESULT = 1001;
	public RecyclerView mRecyclerView;
	public GossipAdapter mAdapter;
	private FlyOutContainer root;
	private static NewGossipActivity mInstance;

	public static NewGossipActivity getInstance() {
		return mInstance;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.new_gossip);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		this.root = (FlyOutContainer) this.getLayoutInflater().inflate(
				R.layout.new_gossip, null);
		this.setContentView(root);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.new_gossip, menu);
		getMenuInflater().inflate(R.menu.actionbar_newgossip, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {

		case R.id.action_settings:
			openSettings();
			return true;
		case R.id.menu_button:
			toggleMenu(mRecyclerView);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void toggleMenu(View v) {
		this.root.toggleMenu();
	}

	private void openSearch() {
		// TODO Auto-generated method stub

	}

	private void openSettings() {
		Intent intent = new Intent(this, Settings.class);
		startActivity(intent);
	}
	public void mainGossip(View v) {
		Intent i = new Intent(this, MainActivity.class);
		startActivity(i);
	}

	public void submitNewGossip(View v) {

		TextView tv = (TextView) findViewById(R.id.recipient);
		String rec = tv.getText().toString();
		tv = (TextView) findViewById(R.id.gossip);
		String text = tv.getText().toString();
		RadioGroup rg = (RadioGroup) findViewById(R.id.radio);
		Emotion emotion;
		switch (rg.getCheckedRadioButtonId()) {
		case R.id.radio_happy:
			emotion = Emotion.GLUECKLICH;
			break;
		case R.id.radio_sad:
			emotion = Emotion.TRAURIG;
			break;
		case R.id.radio_angry:
			emotion = Emotion.WUETEND;
			break;
		case R.id.radio_disappointed:
			emotion = Emotion.ENTAEUSCHT;
			break;
		case R.id.radio_amused:
			emotion = Emotion.BELUSTIGT;
			break;
		default:
			emotion = Emotion.GLUECKLICH;
			break;
		}

		SharedPreferences gossip = getSharedPreferences(PREFS_NAME,
				MODE_PRIVATE);
		int key = gossip.getInt("key", 0);
		SharedPreferences.Editor editor = gossip.edit();
		key++;
		editor.putInt("key", key);
		editor.putString("gossip" + key + "text", text);
		editor.putString("gossip" + key + "emotion", emotion.toString());
		editor.putString("gossip" + key + "rec", rec);
		editor.putLong("gossip" + key + "date", new Date().getTime());
		editor.commit();
		System.out.println("commited");
		System.out.println("new key: " + key);

		Intent i = new Intent(this, MyGossipsActivity.class);
		i.putExtra("key", key);
		startActivity(i);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case CONTACT_PICKER_RESULT:
				ReadContactData(data);
				break;
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	public void getUser(View v) {
		Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
				Contacts.CONTENT_URI);
		startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
	}

	private void ReadContactData(Intent data) {
		Uri dataUri = data.getData();
		// String contactId = dataUri.getLastPathSegment();
		String name = null;

		// Cursor for name

		Cursor cursor = getContentResolver().query(dataUri, null, null, null,
				null);

		if (cursor.moveToFirst()) {

			name = cursor.getString(cursor
					.getColumnIndex(Contacts.DISPLAY_NAME));

		}
		TextView tv = (TextView) findViewById(R.id.recipient);
		tv.setText(name);
	}

}
